package com.np.np_android;

import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.bumptech.glide.Glide;
import com.np.np_android.model.OrderSheet;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class ScrollingActivity extends AppCompatActivity implements View.OnClickListener {

    private Toolbar mToolbar;
    private CollapsingToolbarLayout mToolbarLayout;
    private AppBarLayout mAppBar;
    private TextView mTextBrand;
    private TextView mTextClassify;
    private TextView mTextNumber;
    private TextView mTextIntroduce;
    private FloatingActionButton mFab;
    private ImageView imgPrd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scrolling);
        initView();
        initData();
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
//                        .setAction("Action", null).show();
                new AlertDialog.Builder(ScrollingActivity.this).setTitle("是否购买并付款")
                        .setNegativeButton("没钱，不买", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {

                            }
                        }).setPositiveButton("买买买！", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {

                        final RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
                        StringRequest stringRequest = new StringRequest(Request.Method.GET,Api.ADD_ORDER,
                                new Response.Listener<String>() {
                                    @Override
                                    public void onResponse(String response) {
                                        Log.d("TAG", "response -> " + response);

                                            Toast.makeText(ScrollingActivity.this, "下单成功", Toast.LENGTH_SHORT).show();

                                    }
                                }, new Response.ErrorListener() {
                            @Override
                            public void onErrorResponse(VolleyError error) {
                                Log.e("TAG", error.getMessage(), error);

                            }
                        }) {
                            @Override
                            protected Map<String, String> getParams() {
                                //在这里设置需要post的参数
                                Map<String, String> map = new HashMap<String, String>();
                                map.put("uid",""+Temp.user.getId());
                                map.put("pid",""+Temp.product.getId());
                                map.put("orderStatus","未付款");
                                map.put("orderTime",new Date().toString());
                                map.put("address", "盐城工学院");
                                map.put("number","1");
                                return map;
                            }
                        };
                        requestQueue.add(stringRequest);


                    }
                }).create().show();
            }
        });
    }

    private void initData() {
        mTextBrand.setText("品牌："+Temp.product.getBrand());
        mTextClassify.setText("类别："+Temp.product.getClassifty());
        mTextIntroduce.setText("介绍："+Temp.product.getIntroduce());
        mTextNumber.setText("库存："+Temp.product.getStore());
        mToolbar.setTitle(Temp.product.getName());
        Log.d("TAG", "initData: "+Temp.product.getName());

        String imgName = Temp.product.getImgPath();
        imgName.replace("../img/","");
        Log.d("TAG", "initData: "+Api.DOWNLOAD_IMG+imgName);
        Glide.with(this)
                .load(Api.DOWNLOAD_IMG+imgName)
                .into(imgPrd);

    }

    private void initView() {
        mToolbar = (Toolbar) findViewById(R.id.toolbar);
        mToolbarLayout = (CollapsingToolbarLayout) findViewById(R.id.toolbar_layout);
        mAppBar = (AppBarLayout) findViewById(R.id.app_bar);
        mTextBrand = (TextView) findViewById(R.id.text_brand);
        mTextClassify = (TextView) findViewById(R.id.text_classify);
        mTextNumber = (TextView) findViewById(R.id.text_number);
        mTextIntroduce = (TextView) findViewById(R.id.text_introduce);
        mFab = (FloatingActionButton) findViewById(R.id.fab);
        imgPrd = (ImageView) findViewById(R.id.img_product);

        mFab.setOnClickListener(this);



    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.fab:

                break;
        }
    }
}
